<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 2015/11/29
 * Time: 13:47
 * 日程表方法
 */

//名字空间
namespace app\gwbn\controllers;

use modules\gwbn\CalendarModule;

class   Calendar extends Admin
{
    //首页方法
    function index()
    {
        $this->display($this->data);
    }

    //添加日程表方法
    function  addCalendar()
    {
        //提交检测
        if ($this->is_post()) {

            //注册模块
            $ADMIN = new CalendarModule();

            //发送数据给CalendarModule模块里的addCalendar函数处理并取返回值赋予add_c_e
            $this->data['add_c_e'] = $ADMIN->addCalendar($_POST);
        }
        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //查看日程表方法
    function  queryCalendar()
    {
        //注册模块
        $ADMIN = new CalendarModule();

        //发送数据给CalendarModule模块里的queryCalendar函数处理并取返回值赋予query
        $calendar = $ADMIN->queryCalendar();


        foreach($calendar as &$k){
            $k['num'] = 0;
            if ($k['start'] == 0){
                $k['num'] = $ADMIN->list_num($k['entry']);
            }
        }

        //返回数组给query
        $this->data['query'] = $calendar;


        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //获取任务跟踪列表
    function queryList()
    {
        //注册模块
        $ADMIN = new CalendarModule();

        if ($this -> is_post()){

            //获取任务编号
            $id = $_POST['value'];

            //获取任务列表
            $qlist = $ADMIN->query_list($id);

            if ($qlist){

                //返回JSON格式的列表数据
                return json_encode($qlist);
            }
            else{
                echo "0";
            }

        }
    }

    //确认&更新任务
    function queryset()
    {
        //注册模块
        $ADMIN = new CalendarModule();

        if ($this -> is_post()){

            $sql = $_POST['start'] == "set" ?
                $data = array(
                    'id' => $_POST['id'],
                    'text' => $_POST['text'],
                    'name' => $_SESSION['uname'],
                    'start' => $_POST['start']
                ) :
                $data = array(
                    'id' => $_POST['id'],
                    'start' => $_POST['start']
                );

            return $ADMIN->query_set($sql);
        }
    }
}